package org.example.chm;/*
 * title:
 * author:
 * date:
 * description:
 * */


import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.microsoft.chm.ChmParser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class CHM {
    public static void main(String[] args) {
        // 替换为你本地的CHM文件路径
        String chmFilePath = "C:\\Users\\11924\\Desktop\\SQLyog.chm";

        try (FileInputStream inputStream = new FileInputStream(new File(chmFilePath))) {
            // 创建 Tika 实例
            Tika tika = new Tika();

            // 创建 ChmParser 实例
            ChmParser chmParser = new ChmParser();

            // 创建处理器和上下文
            BodyContentHandler handler = new BodyContentHandler(-1);
            Metadata metadata = new Metadata();
            ParseContext parseContext = new ParseContext();

            // 解析 CHM 文件
            chmParser.parse(inputStream, handler, metadata, parseContext);

            // 输出解析内容
            System.out.println("Contents of the CHM file:");
            System.out.println(handler.toString());

            // 输出元数据
            System.out.println("Metadata of the CHM file:");
            String[] metadataNames = metadata.names();
            for (String name : metadataNames) {
                System.out.println(name + ": " + metadata.get(name));
            }

        } catch (IOException | TikaException | SAXException e) {
            e.printStackTrace();
        }
    }
}
